package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Person person = new Person("李四",33);
        System.out.println(person);

        Class cls = Class.forName("reflect.Person");
        /*
            Constructor反射对象之一，它的每一个实例用于表示某个类中的某个构造器
         */
//        Constructor c = cls.getConstructor();//获取无参构造器
        //Person(String,int)
        Constructor c = cls.getConstructor(String.class,int.class);//获取无参构造器
        //new Person("王五",66)
        Object obj = c.newInstance("王五",66);
        System.out.println(obj);


        //Person(String)
        Constructor c2 = cls.getConstructor(String.class);
        Object obj2 = c2.newInstance("赵六");
        System.out.println(obj2);
    }
}
